import pandas as pd

from data_storage import DataStorage

class TaskManagement:
    def __init__(self):
        self.data_storage = DataStorage()

    def add_task(self, title, description, status, due_date, created_by):
        tasks_df = self.data_storage.read_tasks()
        new_id = len(tasks_df) + 1
        new_task = {'id': new_id, 'title': title, 'description': description, 'status': status, 'due_date': due_date, 'created_by': created_by}
        new_row = pd.DataFrame([new_task])
        tasks_df = pd.concat([tasks_df, new_row], ignore_index=True)
        self.data_storage.write_tasks(tasks_df)

    def update_task(self, task_id, title=None, description=None, status=None, due_date=None):
        tasks_df = self.data_storage.read_tasks()
        if task_id not in tasks_df['id'].values:
            return False, "Task not found"
        task = tasks_df[tasks_df['id'] == task_id].iloc[0]
        if title is not None:
            task['title'] = title
        if description is not None:
            task['description'] = description
        if status is not None:
            task['status'] = status
        if due_date is not None:
            task['due_date'] = due_date
        tasks_df.loc[tasks_df['id'] == task_id, task.index] = task
        self.data_storage.write_tasks(tasks_df)
        return True, "Task updated successfully"

    def delete_task(self, task_id):
        tasks_df = self.data_storage.read_tasks()
        if task_id not in tasks_df['id'].values:
            return False, "Task not found"
        tasks_df = tasks_df[tasks_df['id'] != task_id]
        self.data_storage.write_tasks(tasks_df)
        return True, "Task deleted successfully"

    def get_tasks(self):
        return self.data_storage.read_tasks()

    def generate_report(self):
        tasks_df = self.data_storage.read_tasks()
        report = tasks_df.to_html(index=False)
        with open('tasks_report.html', 'w', encoding='utf-8') as f:
            f.write(report)